//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLHomePresentViewController.h"
#define goldColor [UIColor colorWithRed:225/255.0 green:228/255.0 blue:143/255.0 alpha:1]
@interface KLHomePresentViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIImageView                     *customView;
@property(nonatomic, strong) NSArray                     *buttonArray;
@property(nonatomic, copy) void(^buttonAction)(NSString* title)                    ;
@end

@implementation KLHomePresentViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
- (instancetype)initWithHandle:(void (^)(NSString *))handle{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
        self.buttonAction = handle;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIImageView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth - 60, 342)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor colorWithRed:147/255.0 green:93/255.0 blue:52/255.0 alpha:1];
    self.customView.layer.cornerRadius = 10;
    self.customView.clipsToBounds = YES;
    self.customView.image = [UIImage imageNamed:@"home_dialog"];
    [self.clearButton addSubview:self.customView];
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedWhiteView)];
    self.customView.userInteractionEnabled = YES;
    [self.customView addGestureRecognizer:ges];
    
    //sub
    UIColor * color = goldColor;
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, self.customView.width, 35)
                                                     text:@"更多"
                                                textColor:FirstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.backgroundColor = color;
    [self.customView addSubview:titleLable];
    CGFloat buttonTop = 50;
    STButton  * carButton = [self delfultButtonWithTitle:@"货运信息"];
    carButton.left = 35;
    carButton.top = buttonTop;
  //  carButton.backgroundColor = KL_BlueBackGroundColor;
  //  carButton.selected = YES;
    //[carButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
   // [carButton st_setBorderWith:1 borderColor:KL_BlueBackGroundColor cornerRadius:5];
    [self.customView addSubview:carButton];

    STButton  * chuzuButton = [self delfultButtonWithTitle:@"民车出租"];
    chuzuButton.right =self.customView.width -  35;
    chuzuButton.top = buttonTop;
    [self.customView addSubview:chuzuButton];
    
    STButton  * yishengButton = [self delfultButtonWithTitle:@"良医治病"];
    yishengButton.left = 35;
    yishengButton.top = buttonTop + carButton.bottom;
    [self.customView addSubview:yishengButton];
    
    STButton  * weekButton = [self delfultButtonWithTitle:@"患者求医"];
    weekButton.right = self.customView.width -  35;
    weekButton.top = buttonTop+ carButton.bottom;
    [self.customView addSubview:weekButton];
    
    STButton  * hotelButton = [self delfultButtonWithTitle:@"酒店住宿"];
    hotelButton.left =  35;
    hotelButton.top = buttonTop + weekButton.bottom;
    [self.customView addSubview:hotelButton];
    
    STButton  * fandianButton = [self delfultButtonWithTitle:@"饭店订桌"];
    fandianButton.right =   self.customView.width -  35;
    fandianButton.top = buttonTop + weekButton.bottom;
    [self.customView addSubview:fandianButton];
    
    self.buttonArray = @[carButton,chuzuButton,yishengButton,weekButton];
    self.customView.height = hotelButton.bottom + 40;

}
- (STButton*)delfultButtonWithTitle:(NSString*)title{
    STButton * carButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 80, 35)
                                                     title:title
                                                titleColor:FirstTextColor
                                                 titleFont:15
                                              cornerRadius:18
                                           backgroundColor:goldColor
                                           backgroundImage:nil
                                                     image:nil];
    //[UIImage imageNamed:@"矩形7拷贝"] [UIImage imageNamed:title]
    carButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    __weak typeof(self) weakSelf =  self;
    [carButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedMenuButton:sender];
    }];
    return carButton;
}
#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.centerY = UIScreenHeight / 2 - 40;;

                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;

    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
#pragma mark --Action Method
- (void)onSelectedMenuButton:(UIButton*)sender{
    for (UIButton * button in self.buttonArray) {
        button.selected = NO;
        sender.selected = YES;
        if (button.selected) {
//            button.backgroundColor = KL_BlueBackGroundColor;
//            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
//             [button st_setBorderWith:1 borderColor:KL_BlueBackGroundColor cornerRadius:5];
            [button setBackgroundImage:[UIImage imageNamed:@"矩形7"] forState:UIControlStateSelected];
        }else{
//            button.backgroundColor = [UIColor clearColor];
//            [button setTitleColor:KL_firstTextColor forState:UIControlStateNormal];
//            [button st_setBorderWith:1 borderColor:KL_thirdTextColor cornerRadius:5];
            [button setBackgroundImage:[UIImage imageNamed:@"矩形7拷贝"] forState:UIControlStateNormal];
        }
    }
    
    if (self.buttonAction) {
        self.buttonAction(sender.currentTitle);
        [self dismiss];
    }
   
}

- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}
@end



